package com.shuzijun.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.HashSet;

public class IntersectionOfTwoArrays{
    public static void main(String[] args) {
         Solution solution = new IntersectionOfTwoArrays().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }

        ArrayList<Integer> result = new ArrayList<>();
        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])){
                set.remove(nums2[i]);
                result.add(nums2[i] );
            }
        }
        int[] resultArr = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            resultArr[i] = result.get(i);
        }

        return resultArr;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}